# hello Makefile
# Albert Cahalan, 2002-2007

VERSION      := 1
SUBVERSION   := 0
MINORVERSION := 0
TARVERSION   := $(VERSION).$(SUBVERSION).$(MINORVERSION)

TARFILES := README COPYING Makefile hello.c dummy.c activity.info an_icon.svg

# Preprocessor flags.
PKG_CPPFLAGS := -D_GNU_SOURCE -I/usr/X11R6/include
CPPFLAGS     :=
ALL_CPPFLAGS := $(PKG_CPPFLAGS) $(CPPFLAGS)

# Since none of the PKG_CFLAGS things are truly required
# to compile hello, they might best be moved to CFLAGS.
# On the other hand, they aren't normal -O -g things either.
#
# Note that -O2 includes -fomit-frame-pointer only if the arch
# doesn't lose some debugging ability.
#
PKG_CFLAGS   := -fno-common -ffast-math \
  -W -Wall -Wshadow -Wcast-align -Wredundant-decls \
  -Wbad-function-cast -Wcast-qual -Wwrite-strings -Waggregate-return \
  -Wstrict-prototypes -Wmissing-prototypes
# Note that some stuff below is conditional on CFLAGS containing
# an option that starts with "-g". (-g, -g2, -g3, -ggdb, etc.)
CFLAGS       := -O2 -s
ALL_CFLAGS   := $(PKG_CFLAGS) $(CFLAGS)

PKG_LDFLAGS  := -Wl,-warn-common -L/usr/X11R6/lib -lX11
LDFLAGS      :=
ALL_LDFLAGS  := $(PKG_LDFLAGS) $(LDFLAGS)

############ Add some extra flags if gcc allows

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),tar)  

# Unlike the kernel one, this check_gcc goes all the way to
# producing an executable. There might be a flag that works
# fine until you go looking for a library.
check_gcc = $(shell if $(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) dummy.c $(ALL_LDFLAGS) $(1) -o /dev/null > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)

ALL_CFLAGS += $(call check_gcc,-m32,)

ALL_CFLAGS += $(call check_gcc,-march=geode -mmmx -m3dnow,$(call check_gcc,-march=athlon -mtune=generic -mmmx -m3dnow,))

# if not debugging, enable things that could confuse gdb
ifeq (,$(findstring -g,$(filter -g%,$(CFLAGS))))
ALL_CFLAGS += $(call check_gcc,-fweb,)
ALL_CFLAGS += $(call check_gcc,-frename-registers,)
ALL_CFLAGS += -fomit-frame-pointer
endif

# in case -O3 is enabled, avoid bloat
ALL_CFLAGS += $(call check_gcc,-fno-inline-functions,)

endif
endif

############ misc.

.SUFFIXES:
.SUFFIXES: .a .o .c .s .h

.PHONY: all clean install tar

# want this rule first
all: xo

# don't want to type "make hello-$(TARVERSION).tar.gz"
tar: $(TARFILES)
	mkdir hello-$(TARVERSION)
	(tar cf - $(TARFILES)) | (cd hello-$(TARVERSION) && tar xf -)
	tar cf hello-$(TARVERSION).tar hello-$(TARVERSION)
	gzip -9 hello-$(TARVERSION).tar

# Actually make the *.xo file.
#
# WTF? The Activity_bundles spec says the icon goes in the base directory,
# but all existing apps (as of OS build 406) use the activity directory.
#
# How do I sign this?
#
xo: hello
	mkdir -p hello.activity/activity
	cp an_icon.svg hello.activity/activity/
	cp activity.info hello.activity/activity/
	cp hello hello.activity/
	zip -r - hello.activity > hello-$(shell grep ^activity_version activity.info | sed -r 's/.*= *//').xo

clean:
	rm -rf DEADJOE *~ *.o core gmon.out hello hello-*.xo hello.activity

###### install

install: xo
	mkdir -p $(DESTDIR)/usr/share/activities && cp -a hello.activity $(DESTDIR)/usr/share/activities

############ prog.c --> prog.o

#hello.o : hello.h

%.o : %.c
	$(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) -c -o $@ $<

############ prog.o --> prog

hello: % : %.o
	$(CC) $(ALL_CFLAGS) $^ $(ALL_LDFLAGS) -o $@
